package cn.com.dcsgo.library.client.controller;

import cn.com.dcsgo.library.client.domain.dto.PageDTO;
import cn.com.dcsgo.library.client.domain.query.BookPageQuery;
import cn.com.dcsgo.library.client.domain.vo.BookOverviewVO;
import cn.com.dcsgo.library.client.domain.vo.BookVO;
import cn.com.dcsgo.library.client.response.ResponseResult;
import cn.com.dcsgo.library.client.service.IBooksService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 图书相关接口
 *
 * @author Dcsgo
 * @since 2024-09-02
 */
@RestController
@RequestMapping("/books")
@Api(tags = "图书相关接口")
@RequiredArgsConstructor
@Slf4j
public class BooksController {
    private final IBooksService booksService;

    @GetMapping("{id}")
    @ApiOperation("通过图书编号获取图书详细信息")
    public ResponseResult<BookVO> queryBookById(@PathVariable Long id) {
        try {
            return ResponseResult.success(booksService.queryBookDetails(id));
        } catch (IllegalArgumentException iae) {
            return ResponseResult.fail(null, iae.getMessage());
        }
    }

    @GetMapping("page")
    @ApiOperation("分页查询获取图书信息")
    public ResponseResult<PageDTO<BookOverviewVO>> queryBookByPage(BookPageQuery pageQuery) {
        try {
            return ResponseResult.success(booksService.queryBookByPage(pageQuery));
        } catch (IllegalArgumentException iae) {
            return ResponseResult.fail(null, iae.getMessage());
        }
    }
}